/*******************************************************************************
 * Copyright (c) 2011 Technische Universität Darmstadt
 * 					  Fachbereich Informatik
 * 					  Theoretische Informatik - Kryptographie und Computeralgebra
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

window.addEventListener ( "messagepane-loaded" , function(){
	document.getElementById("messagepanebox").addEventListener("DOMContentLoaded", initOverlay, true);
} , true ) ;

function initOverlay()
{
	//check if subjects should be decrypted:
	var decrypt = true;
	if(info.version.charAt(0)<5){
		decrypt = navigator.preference('cryptoBird.decrypt_subject'); 
	
	}else{	
		  try{
			  decrypt = Services.prefs.getBoolPref('cryptoBird.decrypt_subject');
		  }catch(e){
		  }
	}
	if(decrypt){ 
	var win = Components.classes["@mozilla.org/appshell/window-mediator;1"].
	    getService(Components.interfaces.nsIWindowMediator).
	    getMostRecentWindow("mail:3pane");
	 var msg = win.gFolderDisplay.selectedMessage;
	 if(msg != null){
		 var header = msg.QueryInterface(Components.interfaces.nsIMsgDBHdr);
		 var uri = header.folder.getUriForMsg(header);
		 var dbSubject = win.TheChasingDragon.cryptoBirdJava.decryptSubject(uri);
		 if(dbSubject == "---ERROR---" || dbSubject == ""){
			 document.getElementById('expandedsubjectRow').collapsed = false;
			 document.getElementById('decryptedsubjectRow').collapsed = true;
			 document.getElementById('CB_lockIcon').hidden=true;
		 }else{
			 document.getElementById('decryptedsubjectvalue').value= dbSubject;
			 document.getElementById('decryptedsubjectvalue').style.fontWeight = "bold";
			 document.getElementById('CB_lockIcon').hidden=false;
			 document.getElementById('CB_lockIcon').style.width=14;
			 document.getElementById('CB_lockIcon').flex =1;
			 document.getElementById('decryptedsubjectRow').collapsed = false;
			 document.getElementById('expandedsubjectRow').collapsed = true;
		 }
		 return;
	  }
	}
	document.getElementById('expandedsubjectRow').collapsed = false;
	document.getElementById('decryptedsubjectRow').collapsed = true;
	document.getElementById('CB_lockIcon').hidden=true;
}
